package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageBean;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工类的controller
 *              前后端分离的接口
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 根据id查询单个对象：get localhost:8080/employee/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee findOne(@PathVariable("id") Long id) {
        return employeeService.selectById(id);
    }

    /**
     * 查询所有对象：get localhost:8080/employee
     * @return
     */
    @GetMapping
    public List<Employee> findAll() {
        return employeeService.selectAll();
    }

    /**
     * 添加和修改：Put localhost:8080/employee
     * @param employee
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.save(employee);
            } else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！");
        }
    }

    /**
     * 删除：Delete localhost:8080/employee
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            employeeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败！");
        }
    }

    /**
     * 分页查询：post localhost:8080/employee
     * @param employeeQuery
     * @return
     */
    @PostMapping
    public PageBean<Employee> pageQuery(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.queryPage(employeeQuery);
    }



}